#pragma once
#include "vector"
#include "fstream"
#include "string"
using namespace std;
#include"RobotLogic.h"
#include"Pyramid.h"

struct NodeCell
{
	bool connected;
	bool visited;
	//bool checked;
	int mummy;
	int pit;
	NodeCell()
	{
		connected = false;
		visited = false;
		//checked = false;
		mummy = -1;
		pit = -1;
	}
	bool isOK()
	{
		return (mummy==0 && pit ==0);
	};
};
struct NodeState
{
	int x;
	int y;
	int cost;
	int direction;
	void set(int _x,int _y,int _cost,int _direction)
	{
		x = _x;
		y = _y;
		cost = _cost;
		direction = _direction;
	};
};
struct NodeCellMatrix
{
	bool available;
	int gLeft;
	int gRight;
	int gUp;
	int gDown;

	int lastDirectionLeft;
	int lastDirectionRight;
	int lastDirectionUp;
	int lastDirectionDown;
	NodeCellMatrix()
	{
		available = false;
		gLeft = 10000;
		gRight = 10000;
		gUp = 10000;
		gDown = 10000;
		lastDirectionLeft = -1;
		lastDirectionRight = -1;
		lastDirectionUp = -1;
		lastDirectionDown = -1;
	}

};
class Robot
{

private:
	int nRow;
	int nCol;
	Pyramid map;
	RobotLogic logic;
	vector<string> listAction;
	vector<vector<NodeCell>> forwardChainingMap;
	int nArowLeft;
	int direction;
	int turnCost;
	int X;
	int Y;
	bool checkCell(int x,int y);

	void detectCell(int x,int y);
	void setConnected(int x,int y);
	string createLogicString(string name,int x,int y);
	bool findNewCell();
	int getTurnCost(int ,int);
	int toTheLeft(int direction);
	int toTheRight(int direction);
	void turnAndGoForward(int targetDir);

	int mummyX;
	int mummyY;
	bool huntingModeOn;
	
	void shootTheMummy();

public:
	static int const LEFT = 0;
	static int const DOWN = 1;
	static int const RIGHT = 2;
	static int const UP = 3;

	int getCost();
	int update();
	Robot(void);
	~Robot(void);
	vector<string> getListAction();
	void loadKB(char* fileName);
	void loadMap(char* fileName);
	bool setStartPoint(int x,int y);
	void forward();
	void turnLeft();
	void turnRight();
	bool shoot();

	void print()
	{
		for(int i=0;i<nRow;i++)
		{
			cout<<endl;
			for(int j=0;j<nCol;j++)
			{
				if(forwardChainingMap[i][j].connected)
				{
					if(forwardChainingMap[i][j].mummy == 1)
						cout<<"M ";
					else if(forwardChainingMap[i][j].pit == 1)
						cout<<"P ";
					else if(forwardChainingMap[i][j].isOK())
						cout<<". ";
					else
						cout<<"@ ";
				}
				else
				{
					cout<<"# ";
				}
			}
		}
	}


};

